﻿//using System;
//using System.Collections.Generic;
//using System.Text.RegularExpressions;
////using Microsoft.Office.Interop.Visio;
//using Path=System.IO.Path;

//namespace CleanSVG
//{
//    internal class VisioController
//    {
//        private const short VBFalse = 0;
//        private const short VBTrue = -1;
//        private readonly CleanSVG.StatusMessager StatusMessage;

//        public VisioController(CleanSVG.StatusMessager messenger)
//        {
//            StatusMessage = messenger;
//        }

//        public List<string> ConvertVsdToSvgs(string fileName, bool savePNGReference)
//        {
//            var svgFiles = new List<string>();

//            try
//            {
//                StatusMessage("Starting Visio to convert {0}...", Path.GetFileName(fileName));
//                var appVisio = new Application();
//                var visioDoc = appVisio.Documents.OpenEx(fileName,
//                                                              ((short) VisOpenSaveArgs.visOpenRO) |
//                                                              ((short) VisOpenSaveArgs.visOpenMacrosDisabled) |
//                                                              ((short) VisOpenSaveArgs.visOpenDontList));
//                var pages = visioDoc.Pages;
//                var numPages = pages.Count;

//                for (var i = 0; i < numPages; ++i)
//                {
//                    var thePage = pages[i + 1]; // OLE automation collections are 1-based like VB
//                    var pageName = thePage.Name;

//                    //	go to the page if it's not the current page
//                    if (appVisio.ActivePage.Name != pageName)
//                        appVisio.ActiveWindow.Page = thePage;

//                    //  SVG exporter won't export background pages (but the PNG exporter does)
//                    if (thePage.Background == VBTrue)
//                    {
//                        StatusMessage("\tBackground page {0} skipped.", pageName);
//                        continue;
//                    }

//                    //	skip pages with no shapes
//                    appVisio.ActiveWindow.SelectAll();

//                    if (appVisio.ActiveWindow.Selection.Count < 1)
//                    {
//                        StatusMessage("\tPage {0} skipped because it contains no shapes.", pageName);
//                        continue;
//                    }

//                    appVisio.ActiveWindow.DeselectAll();

//                    //	PNG exporter exports the bounds of the shapes on the page
//                    //	SVG exporter exports the page size
//                    //	We used to try to size the drawing page to the contents of the drawing but that
//                    //	feature had bugs (not all objects in the drawing were properly updated).
//                    //	Moreover, it required the use of SendKeys to invoke in Visio 2007 and that has
//                    //	its own set of problems. On top of that, the SendKeys solution didn't work in
//                    //	Visio 2010 because the option's been removed from Page Setup.

//                    //	here, we warn the user about non-printing shapes that won't appear in the PNG but will in the SVG
//                    var shapes = thePage.Shapes;
//                    var numShapes = shapes.Count;
//                    var nonPrinters = 0;
//                    for (var shapeNum = 1; shapeNum <= numShapes; ++shapeNum)
//                    {
//                        var oneShape = shapes[shapeNum];
//                        if (oneShape.Type == (short) VisShapeTypes.visTypeGuide)
//                            continue;

//                        var noPrint = oneShape.get_CellsU("NonPrinting").FormulaU;

//                        if (string.Compare(noPrint, true.ToString(), true) == 0)
//                            ++nonPrinters;
//                    }

//                    if (nonPrinters > 0)
//                    {
//                        StatusMessage("\tPage {0} contains {1} non-printing shape{2} that will not appear in the reference PNG.",
//                                      pageName, nonPrinters, nonPrinters == 1 ? string.Empty : "s");
//                    }

//                    var pageSuffix = numPages == 1
//                                            ? string.Empty
//                                            : "." +
//                                              Regex.Replace(pageName,
//                                                            @"[\." + Regex.Escape(Path.GetInvalidFileNameChars().ToString()) + "]",
//                                                            string.Empty);
//                    var svgname = string.Format("{0}{1}.svg", fileName, pageSuffix);
//                    thePage.Export(svgname);
//                    StatusMessage("\tSaved {0}", svgname);
//                    svgFiles.Add(svgname);

//                    if (savePNGReference)
//                    {
//                        //  also save a PNG version as a reference copy
//                        var pngname = Regex.Replace(svgname, @"\.svg$", ".png");
//                        thePage.Export(pngname);
//                        StatusMessage("\tSaved {0}", pngname);
//                    }
//                }

//                visioDoc.Saved = true; // lie about the document being saved
//                visioDoc.Close();
//                appVisio.Quit();
//                StatusMessage("Done using Visio to convert {0}.", Path.GetFileName(fileName));
//            }
//            catch (Exception e)
//            {
//                StatusMessage("Error \"{0}\"!" + Environment.NewLine, e.Message);
//                return null;
//            }

//            return svgFiles;
//        }
//    }
//}